﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using HR60_PanelSoft.Model;
using HR60_PanelSoft.Model.BaseModel;
using HR60_PanelSoft.View.Controls;
using HR60_PanelSoft.View.Base;

namespace HR60_PanelSoft.View.TabForms
{
    //GC: [g1]优化编辑bug[PanelForm_Buttons]
    //GC: [g2]联调7-19-02,修复button的displayname不保存[PanelForm_Buttons]
    //GC: [g3]bug#2614[PanelForm_Buttons]
	public partial class PanelForm_Buttons : BaseForm
	{
        /* [g3.s1]定义私有变量，传递ComboBox */

        private ComboBox _DevSelDev = null, _DevSelScen = null, _ScenSelScen = null;
        

        /* end of [g3.s1] */
		public PanelForm_Buttons()
		{
			InitializeComponent();

            /* [g3.s2] */

            _DevSelDev = (ComboBox)(((BaseFormControl)(btnMappingEdit.GetCoreControls().Find(x=>(x.Name.Equals("selDevice"))))).GetCoreControls()[0]);
            _DevSelScen = (ComboBox)(((BaseFormControl)(btnMappingEdit.GetCoreControls().Find(x => (x.Name.Equals("selScene"))))).GetCoreControls()[0]);
            _ScenSelScen = (ComboBox)(((BaseFormControl)(btnSceneMappingEdit.GetCoreControls().Find(x => (x.Name.Equals("selScene"))))).GetCoreControls()[0]);

            if (_DevSelDev == null || _DevSelScen == null || _ScenSelScen == null)
                return;
            _DevSelDev.DropDownClosed += new EventHandler(comboBox_Dev_DropDownClosed);
            _DevSelScen.DropDownClosed += new EventHandler(comboBox_Dev_DropDownClosed);
            _ScenSelScen.DropDownClosed += new EventHandler(comboBox_Scene_DropDownClosed);

            /* end of [g3.s2] */
		}

		// 绑定数据
		public override void refreshFormData(System.Collections.Generic.IDictionary<string, object> dataMap)
		{
			HR60_PanelSoft.Model.Panel panel = (HR60_PanelSoft.Model.Panel)dataMap["panel"];

			if (panel.type.maxPage > 0)
			{
				btnPage.PageCount = panel.type.maxPage;
				btnPage.ButtonCountPerPage = panel.type.buttonsPerPage;
				refreshButtonTable(1);
				btnPage.Visible = true;
			}
			else
			{
				btnPage.Visible = false;
			}


			if (panel.type.maxScenePage > 0)
			{
				scenePage.PageCount = panel.type.maxScenePage;
				scenePage.ButtonCountPerPage = panel.type.buttonsPerPage;
				refreshSceneTable(1);
				scenePage.Visible = true;
			}
			else
			{
				scenePage.Visible = false;
			}

            /* [g1.s1] */
            btnMappingEdit.bindButton(null);

            btnSceneMappingEdit.bindButton(null);
            /* end of [g1.s1] */

		}

		// 数据修改事件
		public override void onFormChanged(BaseFormControl sender, System.EventArgs e)
		{
			// 按钮影射修改
			if (sender == btnMappingEdit)
			{
				object val = btnMappingEdit.Value;
				if (val == null)
				{
					btnPage.setTableCellText(btnPage.SelectedButtonNum, "未定义");
                    //GC: 修正设备按钮页翻页bug
                    HR60_PanelSoft.Model.Panel panel = (HR60_PanelSoft.Model.Panel)bindDataMap["panel"];
                    panel.pages[btnPage.PageNum - 1].buttons[btnPage.SelectedButtonNum - 1].mappingDevice = null;
                    panel.pages[btnPage.PageNum - 1].buttons[btnPage.SelectedButtonNum - 1].mappingScene = null;
                    /* [g2.s2] */
                    /* [g3.s4] displayName应该为null而不是Empty或"" */
                    //panel.pages[btnPage.PageNum - 1].buttons[btnPage.SelectedButtonNum - 1].displayName = string.Empty;
                    panel.pages[btnPage.PageNum - 1].buttons[btnPage.SelectedButtonNum - 1].displayName = null;
                    /* end of [g3.s4] */
                    /* end of [g2.s2] */
				}
				else if (val.GetType().Equals(typeof(Device)))
				{

					//btnPage.setTableCellText(btnPage.SelectedButtonNum, "[设备]" + ((Device)val).nickName);
                    btnPage.setTableCellText(btnPage.SelectedButtonNum, "" + ((Device)val).nickName);

                    //GC: 保存按钮关联到内存
                    /*  */
                    HR60_PanelSoft.Model.Panel panel = (HR60_PanelSoft.Model.Panel)bindDataMap["panel"];
                    panel.pages[btnPage.PageNum - 1].buttons[btnPage.SelectedButtonNum - 1].mappingDevice = ((Device)val).uuid;
                    //GC:解决设备按钮与场景按钮互斥
                    panel.pages[btnPage.PageNum - 1].buttons[btnPage.SelectedButtonNum - 1].mappingScene = null;

                    /* [g2.s5]显示名称去掉[设备] */
                    /* [g2.s1]保存displayname */
                    //panel.pages[btnPage.PageNum - 1].buttons[btnPage.SelectedButtonNum - 1].displayName = "[设备]" + ((Device)val).nickName;
                    panel.pages[btnPage.PageNum - 1].buttons[btnPage.SelectedButtonNum - 1].displayName = ((Device)val).nickName;

                    /* end of [g2.s1] */
                    
				}
				else if (val.GetType().Equals(typeof(Scene)))
				{
					btnPage.setTableCellText(btnPage.SelectedButtonNum, "[场景]" + ((Scene)val).name);

                    //GC: 保存场景按钮关联到内存
                    /*  */
                    HR60_PanelSoft.Model.Panel panel = (HR60_PanelSoft.Model.Panel)bindDataMap["panel"];
                    panel.pages[btnPage.PageNum - 1].buttons[btnPage.SelectedButtonNum - 1].mappingScene = ((Scene)val).uuid;
                    //GC:解决设备按钮与场景按钮互斥
                    panel.pages[btnPage.PageNum - 1].buttons[btnPage.SelectedButtonNum - 1].mappingDevice = null;

                    /* [g2.s5]显示名称去掉[场景] */
                    /* [g2.s1]保存displayname */
                    //panel.pages[btnPage.PageNum - 1].buttons[btnPage.SelectedButtonNum - 1].displayName = "[场景]" + ((Scene)val).name;
                    panel.pages[btnPage.PageNum - 1].buttons[btnPage.SelectedButtonNum - 1].displayName = ((Scene)val).name;
                    /* end of [g2.s1] */
                    /* end of [g2.s5] */


				}
				
			}

			// 场景按钮影射修改
			else if (sender == btnSceneMappingEdit)
			{
				Scene val = (Scene) btnSceneMappingEdit.Value;
				if(val == null)
				{
					scenePage.setTableCellText(scenePage.SelectedButtonNum, "未定义");

                    //GC: 修正场景按钮页翻页bug

                    HR60_PanelSoft.Model.Panel panel = (HR60_PanelSoft.Model.Panel)bindDataMap["panel"];
                    panel.scenePages[scenePage.PageNum - 1].buttons[scenePage.SelectedButtonNum - 1].mappingScene = null;

                    /* [g2.s3] */
                    panel.scenePages[scenePage.PageNum - 1].buttons[scenePage.SelectedButtonNum - 1].displayName = string.Empty;
                    /* end of [g2.s3] */

				}
				else
				{
					scenePage.setTableCellText(scenePage.SelectedButtonNum, val.name);

                    //GC: 保存场景按钮关联到内存

                    HR60_PanelSoft.Model.Panel panel = (HR60_PanelSoft.Model.Panel)bindDataMap["panel"];
                    panel.scenePages[scenePage.PageNum - 1].buttons[scenePage.SelectedButtonNum - 1].mappingScene = ((Scene)val).uuid;

                    /* [g2.s4] */
                    panel.scenePages[scenePage.PageNum - 1].buttons[scenePage.SelectedButtonNum - 1].displayName = val.name;
                    /* end of [g2.s4] */

				}
			}
		}

		// 联动按键表格
		private void refreshButtonTable(int pageNum)
		{
            //GC:修正场景翻页bug
            

			HR60_PanelSoft.Model.Panel panel = (HR60_PanelSoft.Model.Panel)bindDataMap["panel"];

			for (int i = 0; i < btnPage.ButtonCountPerPage; i++)
			{
				PanelButton btn = panel.pages[pageNum - 1].buttons[i];
				if (btn.mappingDevice != null)
				{
					btnPage.setTableCellText(i + 1, "[设备]" + findDeviceByUuid(btn.mappingDevice).nickName);
				}
				else if (btn.mappingScene != null)
				{
					btnPage.setTableCellText(i + 1, "[场景]" + findSceneByUuid(btn.mappingScene).name);
				}
				else
				{
					btnPage.setTableCellText(i + 1, "未定义");
				}
			}
		}

		// 联动场景表格
		private void refreshSceneTable(int pageNum)
		{
			HR60_PanelSoft.Model.Panel panel = (HR60_PanelSoft.Model.Panel)bindDataMap["panel"];

			for (int i = 0; i < btnPage.ButtonCountPerPage; i++)
			{
                //GC: 修改场景页面翻页bug
                /**/
				PanelButton btn = panel.scenePages[pageNum - 1].buttons[i];
				if (btn.mappingScene != null)
				{
					scenePage.setTableCellText(i + 1, "[场景]" + findSceneByUuid(btn.mappingScene).name);
				}
				else
				{
					scenePage.setTableCellText(i + 1, "未定义");
				}
			}
		}

		// 按键页面翻页
		private void btnPage_PageChanged(object sender, EventArgs e)
		{
			isBindDataFlag = false;
			refreshButtonTable(btnPage.PageNum);
			isBindDataFlag = true;
		}

		// 场景页面翻页
		private void scenePage_PageChanged(object sender, EventArgs e)
		{
			isBindDataFlag = false;
			refreshSceneTable(scenePage.PageNum);
			isBindDataFlag = true;
		}

		// 按钮页按钮选择
		private void btnPage_ButtonSelected(object sender, EventArgs e)
		{
			isBindDataFlag = false;
			HR60_PanelSoft.Model.Panel panel = (HR60_PanelSoft.Model.Panel)bindDataMap["panel"];

			if (btnPage.SelectedButtonNum == 0)
			{
				btnMappingEdit.bindButton(null);
			}
			else
			{
				PanelButton btn = panel.pages[btnPage.PageNum - 1].buttons[btnPage.SelectedButtonNum - 1];
				btnMappingEdit.Title = "按钮" + btnPage.SelectedButtonNum;
				btnMappingEdit.bindButton(btn);
			}
			isBindDataFlag = true;
		}

		// 场景页按钮选择
		private void scenePage_ButtonSelected(object sender, EventArgs e)
		{
			isBindDataFlag = false;
			HR60_PanelSoft.Model.Panel panel = (HR60_PanelSoft.Model.Panel)bindDataMap["panel"];

			if (scenePage.SelectedButtonNum == 0)
			{
				btnSceneMappingEdit.bindButton(null);
			}
			else
			{
				PanelButton btn = panel.scenePages[scenePage.PageNum - 1].buttons[scenePage.SelectedButtonNum - 1];
				btnSceneMappingEdit.Title = "按钮" + scenePage.SelectedButtonNum;
				btnSceneMappingEdit.bindButton(btn);
			}
			isBindDataFlag = true;
		}


        /* [g3.s3] */

        private void comboBox_Dev_DropDownClosed(object sender, EventArgs e)
        {
            ComboBox tempCBox = (ComboBox)sender;

            HR60_PanelSoft.Model.Panel tempPnl = (HR60_PanelSoft.Model.Panel)bindDataMap["panel"];
            if (tempPnl == null)
                return;

            if (tempCBox.Parent.Name == "selDevice")
            {
                Device tempDev = (Device)((FormComboBox)tempCBox.Parent).Value;
                //int tempCnt = 0;
                if (tempDev == null)
                    return;

                /* 查重 */
                foreach (PanelPage pp in tempPnl.pages)
                {
                    foreach (PanelButton pb in pp.buttons)
                    {
                        if (pb.mappingDevice == tempDev.uuid)  /* 发现重复 */
                        {
                            View.ViewUtils.showAlert("该设备已被按键绑定！");
                            btnMappingEdit.Value = null;
                        }
                    }
                }
                
            }
            else if (tempCBox.Parent.Name == "selScene")
            {
                Scene tempSce = (Scene)((FormComboBox)tempCBox.Parent).Value;
                if (tempSce == null)
                    return;
                /* 查重 */
                foreach (PanelPage pp in tempPnl.pages)
                {
                    foreach (PanelButton pb in pp.buttons)
                    {
                        if (pb.mappingScene == tempSce.uuid)  /* 发现重复 */
                        {
                            View.ViewUtils.showAlert("该场景已被按键绑定！");
                            btnMappingEdit.Value = null;
                        }
                    }
                }
            }
        }

        private void comboBox_Scene_DropDownClosed(object sender, EventArgs e)
        {
            ComboBox tempCBox = (ComboBox)sender;
            HR60_PanelSoft.Model.Panel tempPnl = (HR60_PanelSoft.Model.Panel)bindDataMap["panel"];
            if (tempPnl == null)
                return;
            if (tempCBox.Parent.Name != "selScene")
                return;
            Scene tempSce = (Scene)((FormComboBox)tempCBox.Parent).Value;
            if (tempSce == null)
                return;
            /* 查重 */
            foreach (PanelPage pp in tempPnl.scenePages)
            {
                foreach (PanelButton pb in pp.buttons)
                {
                    if (pb.mappingScene == tempSce.uuid)  /* 发现重复 */
                    {
                        View.ViewUtils.showAlert("该场景已被按键绑定！");
                        btnSceneMappingEdit.Value = null;
                    }
                }
            }
        }

        /* end of [g3.s3] */

	}
}
